package com.scf.fruit.business.hospital.bo;

import com.github.pagehelper.PageHelper;
import com.scf.fruit.business.hospital.po.request.HospitalReq;
import com.scf.fruit.business.hospital.po.response.HospitalRes;
import com.scf.fruit.hospital.HospitalDto;
import com.scf.fruit.hospital.HospitalService;
import com.scf.fruit.hospital.HospitalVisitDto;
import com.scf.fruit.shiro.FruitUserService;
import com.scf.fruit.user.FruitUserDto;
import com.scf.fruit.util.DataTablesRes;
import com.scf.fruit.util.SpringCglibBeanUtils;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author : 孙超飞
 * @title :
 * @description :
 * @date : 2018-04-20 17:36
 */
@Component
public class HospitalBo {
    @Resource
    private HospitalService hospitalService;
    @Resource
    private FruitUserService fruitUserService;
    private List<HospitalDto> hospitalDtoList=new ArrayList<>();
    public DataTablesRes queryList(HospitalReq req){
        PageHelper.startPage(req.getPageIndex(),req.getLength(),false);
        HospitalDto dto=SpringCglibBeanUtils.convert(req, HospitalDto.class);
        List<HospitalDto> hospitalDtos = hospitalService.queryList(dto);
        Integer count = hospitalService.count(dto);
        hospitalDtoList=hospitalService.queryList(dto);
        return new DataTablesRes(req.getDraw(),count,count,hospitalDtos,null);
    }

    public Boolean update(HospitalReq req){
        String s = SecurityUtils.getSubject().getPrincipal().toString();
        FruitUserDto byMobile = fruitUserService.findByMobile(s);
        req.setSaleName(byMobile.getNickName());
        return hospitalService.update(SpringCglibBeanUtils.convert(req, HospitalDto.class));
    }


    public Boolean insert(HospitalReq req){
        String s = SecurityUtils.getSubject().getPrincipal().toString();
        FruitUserDto byMobile = fruitUserService.findByMobile(s);
        req.setSaleName(byMobile.getNickName());
        return hospitalService.insert(SpringCglibBeanUtils.convert(req, HospitalDto.class));
    }



    /**
     * 导出订单数据
     * @param res
     */
    public void exportOrder(HttpServletResponse res){
        HSSFWorkbook wb = exportInfo(hospitalDtoList);
        try {
            String fileName = new String("入院详情.xls".getBytes("UTF-8"),"ISO-8859-1");
            res.setContentType("application/x-download");
            res.addHeader("Content-Disposition", "attachment;filename="+ fileName);
            OutputStream os = res.getOutputStream();
            wb.write(os);
            os.close();
        } catch (Exception e) {
        }
    }

    /**
     * 设置excel内容
     * @param hospitalDtos
     * @return
     */
    public  HSSFWorkbook exportInfo(List<HospitalDto> hospitalDtos){
        try {
            HSSFWorkbook wb = new HSSFWorkbook();
            HSSFCellStyle headStyle = headStyle(wb);
            HSSFCellStyle contentStyle = contentStyle(wb);
            HSSFSheet sheet_1 =  wb.createSheet("入院详情");
            sheet_1.setDefaultColumnWidth(30);
            HSSFRow headRow_0 = sheet_1.createRow(0);
            CellRangeAddress cra=new CellRangeAddress(0, 0, 0, 8);
            sheet_1.addMergedRegion(cra);


            HSSFRow headRow = sheet_1.createRow(1);
            HSSFCell head_cell_1 = headRow.createCell(0);
            head_cell_1.setCellValue("省");
            head_cell_1.setCellStyle(headStyle);

            HSSFCell head_cell_2 = headRow.createCell(1);
            head_cell_2.setCellValue("市");
            head_cell_2.setCellStyle(headStyle);

            HSSFCell head_cell_3 = headRow.createCell(2);
            head_cell_3.setCellValue("医院名称");
            head_cell_3.setCellStyle(headStyle);

            HSSFCell head_cell_4 = headRow.createCell(3);
            head_cell_4.setCellValue("地址");
            head_cell_4.setCellStyle(headStyle);

            HSSFCell head_cell_5 = headRow.createCell(4);
            head_cell_5.setCellValue("邮政编码");
            head_cell_5.setCellStyle(headStyle);

            HSSFCell head_cell_6 = headRow.createCell(5);
            head_cell_6.setCellValue("电话");
            head_cell_6.setCellStyle(headStyle);

            HSSFCell head_cell_7 = headRow.createCell(6);
            head_cell_7.setCellValue("院长");
            head_cell_7.setCellStyle(headStyle);

            HSSFCell head_cell_8 = headRow.createCell(7);
            head_cell_8.setCellValue("床位数");
            head_cell_8.setCellStyle(headStyle);

            HSSFCell head_cell_9 = headRow.createCell(8);
            head_cell_9.setCellValue("等级");
            head_cell_9.setCellStyle(headStyle);

            HSSFCell head_cell_10 = headRow.createCell(9);
            head_cell_10.setCellValue("销售员");
            head_cell_10.setCellStyle(headStyle);

            HSSFCell head_cell_11 = headRow.createCell(10);
            head_cell_11.setCellValue("代理商一");
            head_cell_11.setCellStyle(headStyle);

            HSSFCell head_cell_12 = headRow.createCell(11);
            head_cell_12.setCellValue("代理商二");
            head_cell_12.setCellStyle(headStyle);

            HSSFCell head_cell_13 = headRow.createCell(12);
            head_cell_13.setCellValue("代理商三");
            head_cell_13.setCellStyle(headStyle);


            HSSFCell head_cell_14 = headRow.createCell(13);
            head_cell_14.setCellValue("代理商四");
            head_cell_14.setCellStyle(headStyle);

            HSSFCell head_cell_15 = headRow.createCell(14);
            head_cell_15.setCellValue("主任姓名");
            head_cell_15.setCellStyle(headStyle);

            HSSFCell head_cell_16 = headRow.createCell(15);
            head_cell_16.setCellValue("目前医院使用产品");
            head_cell_16.setCellStyle(headStyle);

            HSSFCell head_cell_17 = headRow.createCell(16);
            head_cell_17.setCellValue("提单");
            head_cell_17.setCellStyle(headStyle);

            HSSFCell head_cell_18 = headRow.createCell(17);
            head_cell_18.setCellValue("招标");
            head_cell_18.setCellStyle(headStyle);

            HSSFCell head_cell_19 = headRow.createCell(18);
            head_cell_19.setCellValue("物价");
            head_cell_19.setCellStyle(headStyle);

            HSSFCell head_cell_20 = headRow.createCell(19);
            head_cell_20.setCellValue("回款");
            head_cell_20.setCellStyle(headStyle);

            HSSFCell head_cell_21 = headRow.createCell(20);
            head_cell_21.setCellValue("主机装机进院");
            head_cell_21.setCellStyle(headStyle);

            HSSFCell head_cell_22 = headRow.createCell(21);
            head_cell_22.setCellValue("耗材循环使用");
            head_cell_22.setCellStyle(headStyle);

            HSSFCell head_cell_23 = headRow.createCell(22);
            head_cell_23.setCellValue("备注");
            head_cell_23.setCellStyle(headStyle);
            //为内容行添加数据和样式
            SimpleDateFormat dateFormat=new SimpleDateFormat();
            HSSFRow contentRow;
            HSSFCell content_cell_1;
            HSSFCell content_cell_2;
            HSSFCell content_cell_3;
            HSSFCell content_cell_4;
            HSSFCell content_cell_5;
            HSSFCell content_cell_6;
            HSSFCell content_cell_7;
            HSSFCell content_cell_8;
            HSSFCell content_cell_9;
            HSSFCell content_cell_10;
            HSSFCell content_cell_11;
            HSSFCell content_cell_12;
            HSSFCell content_cell_13;
            HSSFCell content_cell_14;
            HSSFCell content_cell_15;
            HSSFCell content_cell_16;
            HSSFCell content_cell_17;
            HSSFCell content_cell_18;
            HSSFCell content_cell_19;
            HSSFCell content_cell_20;
            HSSFCell content_cell_21;
            HSSFCell content_cell_22;
            HSSFCell content_cell_23;
            for (int i = 0; i <hospitalDtos.size(); i++) {
                contentRow= sheet_1.createRow(i+2);

                content_cell_1 = contentRow.createCell(0);
                if(null!=hospitalDtos.get(i).getProvince()) {
                    content_cell_1.setCellValue(String.valueOf(hospitalDtos.get(i).getProvince()));
                }
                content_cell_1.setCellStyle(contentStyle);

                content_cell_2 = contentRow.createCell(1);
                if(null!=hospitalDtos.get(i).getCity()) {
                    content_cell_2.setCellValue(hospitalDtos.get(i).getCity());
                }
                content_cell_2.setCellStyle(contentStyle);

                content_cell_3 = contentRow.createCell(2);
                if(null!=hospitalDtos.get(i).getHospitalName()) {
                    content_cell_3.setCellValue(hospitalDtos.get(i).getHospitalName());
                }
                content_cell_3.setCellStyle(contentStyle);

                content_cell_4 = contentRow.createCell(3);
                if(null!=hospitalDtos.get(i).getAddress()) {
                    content_cell_4.setCellValue(hospitalDtos.get(i).getAddress());
                }
                content_cell_4.setCellStyle(contentStyle);

                content_cell_5 = contentRow.createCell(4);
                if(null!=hospitalDtos.get(i).getPostOfficeCode()) {
                    content_cell_5.setCellValue(hospitalDtos.get(i).getPostOfficeCode());
                }
                content_cell_5.setCellStyle(contentStyle);

                content_cell_6 = contentRow.createCell(5);
                if(null!=hospitalDtos.get(i).getPhoneNumber()) {
                    content_cell_6.setCellValue(hospitalDtos.get(i).getPhoneNumber());
                }
                content_cell_6.setCellStyle(contentStyle);

                content_cell_7 = contentRow.createCell(6);
                if(null!=hospitalDtos.get(i).getDeanName()) {
                    content_cell_7.setCellValue(hospitalDtos.get(i ).getDeanName());
                }
                content_cell_7.setCellStyle(contentStyle);

                content_cell_8 = contentRow.createCell(7);
                if(null!=hospitalDtos.get(i).getBedNum()) {
                    content_cell_8.setCellValue(hospitalDtos.get(i).getBedNum());
                }
                content_cell_8.setCellStyle(contentStyle);

                content_cell_9 = contentRow.createCell(8);
                if(null!=hospitalDtos.get(i).getLevel()) {
                    content_cell_9.setCellValue(hospitalDtos.get(i).getLevel());
                }
                content_cell_9.setCellStyle(contentStyle);


                content_cell_10 = contentRow.createCell(9);
                if(null!=hospitalDtos.get(i).getSaleName()) {
                    content_cell_10.setCellValue(hospitalDtos.get(i).getSaleName());
                }
                content_cell_10.setCellStyle(contentStyle);

                content_cell_11 = contentRow.createCell(10);
                if(null!=hospitalDtos.get(i).getOperatorOne()) {
                    content_cell_11.setCellValue(hospitalDtos.get(i).getOperatorOne());
                }
                content_cell_11.setCellStyle(contentStyle);

                content_cell_12 = contentRow.createCell(11);
                if(null!=hospitalDtos.get(i).getOperatorTwo()) {
                    content_cell_12.setCellValue(hospitalDtos.get(i).getOperatorTwo());
                }
                content_cell_12.setCellStyle(contentStyle);

                content_cell_13 = contentRow.createCell(12);
                if(null!=hospitalDtos.get(i).getOperatorThree()) {
                    content_cell_13.setCellValue(hospitalDtos.get(i).getOperatorThree());
                }
                content_cell_13.setCellStyle(contentStyle);

                content_cell_14 = contentRow.createCell(13);
                if(null!=hospitalDtos.get(i).getOperatorFour()) {
                    content_cell_14.setCellValue(hospitalDtos.get(i).getOperatorFour());
                }
                content_cell_14.setCellStyle(contentStyle);


                content_cell_15 = contentRow.createCell(14);
                if(null!=hospitalDtos.get(i).getDirectorName()) {
                    content_cell_15.setCellValue(hospitalDtos.get(i).getDirectorName());
                }
                content_cell_15.setCellStyle(contentStyle);

                content_cell_16 = contentRow.createCell(15);
                if(null!=hospitalDtos.get(i).getCurrentUserProduct()) {
                    content_cell_16.setCellValue(hospitalDtos.get(i).getCurrentUserProduct());
                }
                content_cell_16.setCellStyle(contentStyle);

                content_cell_17 = contentRow.createCell(16);
                if(null!=hospitalDtos.get(i).getCarryBill()) {
                    content_cell_17.setCellValue(hospitalDtos.get(i).getCarryBill());
                }
                content_cell_17.setCellStyle(contentStyle);

                content_cell_18 = contentRow.createCell(17);
                if(null!=hospitalDtos.get(i).getTender()) {
                    content_cell_18.setCellValue(hospitalDtos.get(i).getTender());
                }
                content_cell_18.setCellStyle(contentStyle);


                content_cell_19 = contentRow.createCell(18);
                if(null!=hospitalDtos.get(i).getThingsPrice()) {
                    content_cell_19.setCellValue(hospitalDtos.get(i).getThingsPrice());
                }
                content_cell_19.setCellStyle(contentStyle);

                content_cell_20 = contentRow.createCell(19);
                if(null!=hospitalDtos.get(i).getRepayment()) {
                    content_cell_20.setCellValue(hospitalDtos.get(i).getRepayment());
                }
                content_cell_20.setCellStyle(contentStyle);

                content_cell_21 = contentRow.createCell(20);
                if(null!=hospitalDtos.get(i).getInstallInto()) {
                    content_cell_21.setCellValue(hospitalDtos.get(i).getInstallInto());
                }
                content_cell_21.setCellStyle(contentStyle);

                content_cell_22 = contentRow.createCell(21);
                if(null!=hospitalDtos.get(i).getCycleUse()) {
                    content_cell_22.setCellValue(hospitalDtos.get(i).getCycleUse());
                }
                content_cell_22.setCellStyle(contentStyle);

                content_cell_23 = contentRow.createCell(22);
                if(null!=hospitalDtos.get(i).getCycleUse()) {
                    content_cell_23.setCellValue(hospitalDtos.get(i).getNote());
                }
                content_cell_23.setCellStyle(contentStyle);

            }
            return wb;
        } catch (Exception e) {
            e.getStackTrace();
        }
        return null;
    }

    /**
     * 创建表头样式
     * @param wb
     * @return
     */

    public HSSFCellStyle headStyle(HSSFWorkbook wb){
        HSSFCellStyle headStyle = wb.createCellStyle();
        HSSFFont headFont = wb.createFont();
        headFont.setFontName("微软雅黑");
        headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        headFont.setColor(HSSFFont.COLOR_RED);
        headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        headStyle.setFont(headFont);
        return headStyle;
    }

    public  HSSFCellStyle topicStyle(HSSFWorkbook wb){
        HSSFCellStyle headStyle = wb.createCellStyle();
        HSSFFont headFont = wb.createFont();
        headFont.setFontName("微软雅黑");
        headFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        headFont.setColor(HSSFFont.COLOR_NORMAL);
        headFont.setFontHeightInPoints((short)22);
        headStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        headStyle.setFont(headFont);
        return headStyle;
    }

    /**
     * 创建内容行样式
     * @param wb
     * @return
     */
    public HSSFCellStyle contentStyle(HSSFWorkbook wb){
        HSSFCellStyle contentStyle = wb.createCellStyle();
        HSSFFont contentFont = wb.createFont();
        contentFont.setFontName("微软雅黑");
        contentFont.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
        contentFont.setColor(HSSFFont.COLOR_NORMAL);
        contentStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        contentStyle.setFont(contentFont);
        return contentStyle;
    }
}
